#include "stdafx.h"
#include "AirlineRep.h"
#include <time.h>

#define ONETIME 1
#define DAILY 2
#define WEEKLY 3

using namespace std;

list<Aircraft>* AirlineRep::aircraftList = new list<Aircraft>();
list<Flight>* AirlineRep::pendingFlights = new list<Flight>();
list<Flight>* AirlineRep::cancelledFlights = new list<Flight>();


/*------------------------------------------------------------
/ Default constructor
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Creates an AirlineRep with blank/null attributes.
/ Parameters: 
/ Return: 
------------------------------------------------------------*/
AirlineRep::AirlineRep(){
	airline = "";
}

/*------------------------------------------------------------
/ Alternate constructor
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Creates an AirlineRep with a given id and password
/ Parameters:	_id:	Unique identifier for this AirlineRep
/				_password:	password user uses to access account	
/ Return: 
------------------------------------------------------------*/
AirlineRep::AirlineRep(string _id, string _password){
	this->setEmployeeID(_id);
	this->setEmployeePassword(_password);
}

/*------------------------------------------------------------
/ Default destructor
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Default destructor for AirlineRep
/ Parameters:
/ Return: 
------------------------------------------------------------*/
AirlineRep::~AirlineRep(){
}

/*------------------------------------------------------------
/ getAirline
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Getter for AirlineRep's airline attribute
/ Parameters:
/ Return: The airline this AirlineRep works for as a string
------------------------------------------------------------*/
string AirlineRep::getAirline(){
	return airline;
}

/*------------------------------------------------------------
/ reqOneTimeInFlight (2.5)
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Adds incoming Flight to list of one-time Flights
/				waiting to be approved by AirportManager
/ Parameters:	flightID:	unique identifier for Flight
/				aircraftID:	unique identifier for Aircraft carrying out the Flight
				srcAirport:	identifier for airport the Flight is leaving from
				arrivalTime:Time Flight is scheduled to arrive at the Gate
				landingTime:Time Flight is scheduled to arrive at the airport
/ Return: int:	0 for failure; 1 for success
------------------------------------------------------------*/
int AirlineRep::reqOneTimeInFlight(string flightID, int aircraftID, string
srcAirport, /*Time* arrivalTime, */Time* landingTime){
	
	int incoming = 1;	//Flight is incoming to the Airport
	int flag = 0;					//flag indicating aircraft with given id has been found
	int prepTime = 0;

	list<Aircraft>::iterator acIt;	//list of Aircraft belonging to ALR

	//new Flight created with info given by ALR interface
	Flight *f = new Flight(flightID, aircraftID, /*arrivalTime, */landingTime, "", 0, srcAirport, incoming, ONETIME);

	//setting AircraftType of Flight so that a GateStand can be allocated later
	//search through list of Aircraft until Aircraft with given id is found
	for (acIt = AirlineRep::aircraftList->begin(); acIt != AirlineRep::aircraftList->end() && !flag; acIt++){
	
		//when Aircraft is found, raise flag and add Flight to its schedule
		if (acIt->getAircraftID() == aircraftID){
			flag = 1;

			f->setType(acIt->getAircraftType());	//so f can tell a GateStand what type of Aircraft it is
			prepTime = acIt->getAircraftType()->getBoardingTime() + acIt->getAircraftType()->getServiceTime();
		}
	}

	Time* t = new Time();
	int prepMins = prepTime%60;
	int prepHours = prepTime/60;
	int prepDays = prepTime/1440;
	int prepMonths = 0;
	int m = landingTime->getMonth();
	int prepYear = 0;
	int y = landingTime->getYear();
	if (m == 2){
		m = prepTime/40320;
	}
	else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
		prepMonths = prepTime/44640;
	}
	else{
		prepMonths = prepTime/43200;
	}

	if (y % 4 == 0){	//leap years mod 4 == 0
		prepYear = prepTime/527040;	//60 mins/hour * 24 hrs/day * 366 days
	}
	else{
		prepYear = prepTime/525600;	//60 mins/hour * 24 hrs/day * 365 days
	}
	
	t->setM(landingTime->getM() - prepMins);
	t->setH(landingTime->getH() - prepHours);
	t->setDate(landingTime->getDate() - prepDays);
	t->setMonth(landingTime->getMonth() - prepMonths);
	t->setYear(landingTime->getYear() - prepYear);

	f->setArrivalTime(t);

	list<Flight>::iterator flightIt, advanceIt;

	int done = 0;

	//new Flight's scheduled arrival time
	int year = landingTime->getYear();
	int month = landingTime->getMonth();
	int date = landingTime->getDate();
	int hour = landingTime->getH();
	int min = landingTime->getM();

	//if list is empty, just add Flight to beginning of list
	if (AirlineRep::pendingFlights->empty()){
		AirlineRep::pendingFlights->push_front(*f);
		return 1;
	}

	//for (flightIt = pendingFlights->begin(), advanceIt = pendingFlights->begin(); flightIt != pendingFlights->end() && !done; flightIt++){
	while(flightIt != AirlineRep::pendingFlights->end() && !done){
		
		int currentYear = flightIt->getArrivalTime()->getYear();
		int currentMonth = flightIt->getArrivalTime()->getMonth();
		int currentDate = flightIt->getArrivalTime()->getDate();
		int currentHour = flightIt->getArrivalTime()->getH();
		int currentMin = flightIt->getArrivalTime()->getM();

		if (year > currentYear){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month > currentMonth){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date > currentDate){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date == currentDate && hour > currentHour){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date == currentDate && hour == currentHour && min > currentMin){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}

		flightIt++;
		advanceIt++;
	}

	AirlineRep::pendingFlights->insert(advanceIt, *f);

	return 1;
}

/*------------------------------------------------------------
/ reqOneTimeOutFlight (2.6)
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Adds outgoing Flight to list of one-time Flights
/				waiting to be approved by AirportManager
/ Parameters:	flightID:	unique identifier for Flight
/				aircraftID:	unique identifier for Aircraft carrying out the Flight
				destAirport:identifier for airport the Flight is leaving from
				departureTime:Time Flight is scheduled to depart the Gate
				takeOffTime:Time Flight is scheduled to depart the airport
/ Return: int:	0 for failure; 1 for success
------------------------------------------------------------*/
int AirlineRep::reqOneTimeOutFlight(string flightID, int aircraftID, string
destAirport, /*Time* departureTime, */Time* takeOffTime){
	
	int incoming = 0;
	int flag = 0;					//flag indicating aircraft with given id has been found
	int prepTime = 0;

	Flight *f = new Flight(flightID, aircraftID, /*departureTime, */takeOffTime, "", 0, destAirport, ONETIME);

	list<Aircraft>::iterator acIt;	//list of Aircraft belonging to ALR
	
	//setting AircraftType of Flight so that a GateStand can be allocated later
	//search through list of Aircraft until Aircraft with given id is found
	for (acIt = AirlineRep::aircraftList->begin(); acIt != AirlineRep::aircraftList->end() && !flag; acIt++){
	
		//when Aircraft is found, raise flag and add Flight to its schedule
		if (acIt->getAircraftID() == aircraftID){
			flag = 1;

			f->setType(acIt->getAircraftType());	//so f can tell a GateStand what type of Aircraft it is
			prepTime = acIt->getAircraftType()->getBoardingTime() + acIt->getAircraftType()->getServiceTime();
		}
	}

	Time* t = new Time();
	int prepMins = prepTime%60;
	int prepHours = prepTime/60;
	int prepDays = prepTime/1440;
	int prepMonths = 0;
	int m = takeOffTime->getMonth();
	int prepYear = 0;
	int y = takeOffTime->getYear();
	if (m == 2){
		m = prepTime/40320;
	}
	else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
		prepMonths = prepTime/44640;
	}
	else{
		prepMonths = prepTime/43200;
	}

	if (y % 4 == 0){	//leap years mod 4 == 0
		prepYear = prepTime/527040;	//60 mins/hour * 24 hrs/day * 366 days
	}
	else{
		prepYear = prepTime/525600;	//60 mins/hour * 24 hrs/day * 365 days
	}
	
	t->setM(takeOffTime->getM() - prepMins);
	t->setH(takeOffTime->getH() - prepHours);
	t->setDate(takeOffTime->getDate() - prepDays);
	t->setMonth(takeOffTime->getMonth() - prepMonths);
	t->setYear(takeOffTime->getYear() - prepYear);

	f->setDepartureTime(t);

	list<Flight>::iterator flightIt, advanceIt;

	int done = 0;

	//new Flight's scheduled departure time
	int year = takeOffTime->getYear();
	int month = takeOffTime->getMonth();
	int date = takeOffTime->getDate();
	int hour = takeOffTime->getH();
	int min = takeOffTime->getM();

	//if list is empty, just add Flight to beginning of list
	if (AirlineRep::pendingFlights->empty()){
		AirlineRep::pendingFlights->push_front(*f);
		return 1;
	}

	//for (flightIt = pendingFlights->begin(), advanceIt = pendingFlights->begin(); flightIt != pendingFlights->end() && !done; flightIt++){
	while(flightIt != AirlineRep::pendingFlights->end()/* && !done*/){
		
		int currentYear = flightIt->getArrivalTime()->getYear();
		int currentMonth = flightIt->getArrivalTime()->getMonth();
		int currentDate = flightIt->getArrivalTime()->getDate();
		int currentHour = flightIt->getArrivalTime()->getH();
		int currentMin = flightIt->getArrivalTime()->getM();

		if (year > currentYear){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month > currentMonth){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date > currentDate){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date == currentDate && hour > currentHour){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date == currentDate && hour == currentHour && min > currentMin){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}

		flightIt++;
		advanceIt++;
	}

	AirlineRep::pendingFlights->insert(advanceIt, *f);

	return 1;
}

/*------------------------------------------------------------
/ reqDailyInFlight (2.1)
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Adds incoming Flight to list of daily Flights
/				waiting to be approved by AirportManager
/ Parameters:	flightID:	unique identifier for Flight
/				aircraftID:	unique identifier for Aircraft carrying out the Flight
				srcAirport:	identifier for airport the Flight is leaving from
				arrivalTime:Time Flight is scheduled to arrive at the Gate
				landingTime:Time Flight is scheduled to arrive at the airport
/ Return: int:	0 for failure; 1 for success
------------------------------------------------------------*/
int AirlineRep::reqDailyInFlight(string flightID, int aircraftID, string
srcAirport, /*Time* arrivalTime, */Time* landingTime){
	
	int incoming = 1;	//Flight is incoming to the Airport
	int flag = 0;					//flag indicating aircraft with given id has been found
	int prepTime = 0;

		
	//new Flight created with info given by ALR interface
	Flight *f = new Flight(flightID, aircraftID, /*arrivalTime, */landingTime, "", 0, srcAirport, incoming, DAILY);

	list<Aircraft>::iterator acIt;	//list of Aircraft belonging to ALR

	//setting AircraftType of Flight so that a GateStand can be allocated later
	//search through list of Aircraft until Aircraft with given id is found
	for (acIt = AirlineRep::aircraftList->begin(); acIt != AirlineRep::aircraftList->end() && !flag; acIt++){
	
		//when Aircraft is found, raise flag and add Flight to its schedule
		if (acIt->getAircraftID() == aircraftID){
			flag = 1;

			f->setType(acIt->getAircraftType());	//so f can tell a GateStand what type of Aircraft it is
		}
	}

	Time* t = new Time();
	int prepMins = prepTime%60;
	int prepHours = prepTime/60;
	int prepDays = prepTime/1440;
	int prepMonths = 0;
	int m = landingTime->getMonth();
	int prepYear = 0;
	int y = landingTime->getYear();
	if (m == 2){
		m = prepTime/40320;
	}
	else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
		prepMonths = prepTime/44640;
	}
	else{
		prepMonths = prepTime/43200;
	}

	if (y % 4 == 0){	//leap years mod 4 == 0
		prepYear = prepTime/527040;	//60 mins/hour * 24 hrs/day * 366 days
	}
	else{
		prepYear = prepTime/525600;	//60 mins/hour * 24 hrs/day * 365 days
	}
	
	t->setM(landingTime->getM() - prepMins);
	t->setH(landingTime->getH() - prepHours);
	t->setDate(landingTime->getDate() - prepDays);
	t->setMonth(landingTime->getMonth() - prepMonths);
	t->setYear(landingTime->getYear() - prepYear);

	f->setArrivalTime(t);

	list<Flight>::iterator flightIt, advanceIt;

	int done = 0;

	//new Flight's scheduled arrival time
	int year = landingTime->getYear();
	int month = landingTime->getMonth();
	int date = landingTime->getDate();
	int hour = landingTime->getH();
	int min = landingTime->getM();

	//if list is empty, just add Flight to beginning of list
	if (AirlineRep::pendingFlights->empty()){
		AirlineRep::pendingFlights->push_front(*f);
		return 1;
	}

	//for (flightIt = pendingFlights->begin(), advanceIt = pendingFlights->begin(); flightIt != pendingFlights->end() && !done; flightIt++){
	while(flightIt != AirlineRep::pendingFlights->end() && !done){
		
		int currentYear = flightIt->getArrivalTime()->getYear();
		int currentMonth = flightIt->getArrivalTime()->getMonth();
		int currentDate = flightIt->getArrivalTime()->getDate();
		int currentHour = flightIt->getArrivalTime()->getH();
		int currentMin = flightIt->getArrivalTime()->getM();

		if (year > currentYear){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month > currentMonth){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date > currentDate){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date == currentDate && hour > currentHour){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date == currentDate && hour == currentHour && min > currentMin){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}

		flightIt++;
		advanceIt++;
	}

	AirlineRep::pendingFlights->insert(advanceIt, *f);

	return 1;
}

/*------------------------------------------------------------
/ reqDailyOutFlight (2.2)
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Adds outgoing Flight to list of daily Flights
/				waiting to be approved by AirportManager
/ Parameters:	flightID:	unique identifier for Flight
/				aircraftID:	unique identifier for Aircraft carrying out the Flight
				destAirport:identifier for airport the Flight is leaving from
				departureTime:Time Flight is scheduled to depart the Gate
				takeOffTime:Time Flight is scheduled to depart the airport
/ Return: int:	0 for failure; 1 for success
------------------------------------------------------------*/
int AirlineRep::reqDailyOutFlight(string flightID, int aircraftID, string
destAirport, /*Time* departureTime, */Time* takeOffTime){
	
	int incoming = 0;
	int flag = 0;					//flag indicating aircraft with given id has been found
	int prepTime = 0;

	Flight *f = new Flight(flightID, aircraftID, /*departureTime, */takeOffTime, "", 0, destAirport, DAILY);

	list<Aircraft>::iterator acIt;	//list of Aircraft belonging to ALR
	
	//setting AircraftType of Flight so that a GateStand can be allocated later
	//search through list of Aircraft until Aircraft with given id is found
	for (acIt = AirlineRep::aircraftList->begin(); acIt != AirlineRep::aircraftList->end() && !flag; acIt++){
	
		//when Aircraft is found, raise flag and add Flight to its schedule
		if (acIt->getAircraftID() == aircraftID){
			flag = 1;

			f->setType(acIt->getAircraftType());	//so f can tell a GateStand what type of Aircraft it is			
		}
	}

	Time* t = new Time();
	int prepMins = prepTime%60;
	int prepHours = prepTime/60;
	int prepDays = prepTime/1440;
	int prepMonths = 0;
	int m = takeOffTime->getMonth();
	int prepYear = 0;
	int y = takeOffTime->getYear();
	if (m == 2){
		m = prepTime/40320;
	}
	else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
		prepMonths = prepTime/44640;
	}
	else{
		prepMonths = prepTime/43200;
	}

	if (y % 4 == 0){	//leap years mod 4 == 0
		prepYear = prepTime/527040;	//60 mins/hour * 24 hrs/day * 366 days
	}
	else{
		prepYear = prepTime/525600;	//60 mins/hour * 24 hrs/day * 365 days
	}
	
	t->setM(takeOffTime->getM() - prepMins);
	t->setH(takeOffTime->getH() - prepHours);
	t->setDate(takeOffTime->getDate() - prepDays);
	t->setMonth(takeOffTime->getMonth() - prepMonths);
	t->setYear(takeOffTime->getYear() - prepYear);

	f->setDepartureTime(t);

	list<Flight>::iterator flightIt, advanceIt;

	int done = 0;

	//new Flight's scheduled departure time
	int year = takeOffTime->getYear();
	int month = takeOffTime->getMonth();
	int date = takeOffTime->getDate();
	int hour = takeOffTime->getH();
	int min = takeOffTime->getM();

	//if list is empty, just add Flight to beginning of list
	if (AirlineRep::pendingFlights->empty()){
		AirlineRep::pendingFlights->push_front(*f);
		return 1;
	}

	//for (flightIt = pendingFlights->begin(), advanceIt = pendingFlights->begin(); flightIt != pendingFlights->end() && !done; flightIt++){
	while(flightIt != AirlineRep::pendingFlights->end()/* && !done*/){
		
		int currentYear = flightIt->getArrivalTime()->getYear();
		int currentMonth = flightIt->getArrivalTime()->getMonth();
		int currentDate = flightIt->getArrivalTime()->getDate();
		int currentHour = flightIt->getArrivalTime()->getH();
		int currentMin = flightIt->getArrivalTime()->getM();

		if (year > currentYear){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month > currentMonth){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date > currentDate){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date == currentDate && hour > currentHour){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date == currentDate && hour == currentHour && min > currentMin){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}

		flightIt++;
		advanceIt++;
	}

	AirlineRep::pendingFlights->insert(advanceIt, *f);

	return 1;
}

/*------------------------------------------------------------
/ reqWeeklyInFlight (2.3)
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Adds incoming Flight to list of weekly Flights
/				waiting to be approved by AirportManager
/ Parameters:	flightID:	unique identifier for Flight
/				aircraftID:	unique identifier for Aircraft carrying out the Flight
				srcAirport:	identifier for airport the Flight is leaving from
				arrivalTime:Time Flight is scheduled to arrive at the Gate
				landingTime:Time Flight is scheduled to arrive at the airport
/ Return: int:	0 for failure; 1 for success
------------------------------------------------------------*/
int AirlineRep::reqWeeklyInFlight(string flightID, int aircraftID, string
srcAirport, /*Time* arrivalTime, */Time* landingTime){
	
	int incoming = 1;	//Flight is incoming to the Airport
	int flag = 0;					//flag indicating aircraft with given id has been found
	int prepTime = 0;

	//new Flight created with info given by ALR interface
	Flight *f = new Flight(flightID, aircraftID, /*arrivalTime, */landingTime, "", 0, srcAirport, incoming, WEEKLY);

	list<Aircraft>::iterator acIt;	//list of Aircraft belonging to ALR
	
	//setting AircraftType of Flight so that a GateStand can be allocated later
	//search through list of Aircraft until Aircraft with given id is found
	for (acIt = AirlineRep::aircraftList->begin(); acIt != AirlineRep::aircraftList->end() && !flag; acIt++){
	
		//when Aircraft is found, raise flag and add Flight to its schedule
		if (acIt->getAircraftID() == aircraftID){
			flag = 1;

			f->setType(acIt->getAircraftType());	//so f can tell a GateStand what type of Aircraft it is
		}
	}

	Time* t = new Time();
	int prepMins = prepTime%60;
	int prepHours = prepTime/60;
	int prepDays = prepTime/1440;
	int prepMonths = 0;
	int m = landingTime->getMonth();
	int prepYear = 0;
	int y = landingTime->getYear();
	if (m == 2){
		m = prepTime/40320;
	}
	else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
		prepMonths = prepTime/44640;
	}
	else{
		prepMonths = prepTime/43200;
	}

	if (y % 4 == 0){	//leap years mod 4 == 0
		prepYear = prepTime/527040;	//60 mins/hour * 24 hrs/day * 366 days
	}
	else{
		prepYear = prepTime/525600;	//60 mins/hour * 24 hrs/day * 365 days
	}
	
	t->setM(landingTime->getM() - prepMins);
	t->setH(landingTime->getH() - prepHours);
	t->setDate(landingTime->getDate() - prepDays);
	t->setMonth(landingTime->getMonth() - prepMonths);
	t->setYear(landingTime->getYear() - prepYear);

	f->setArrivalTime(t);

	list<Flight>::iterator flightIt, advanceIt;

	int done = 0;

	//new Flight's scheduled arrival time
	int year = landingTime->getYear();
	int month = landingTime->getMonth();
	int date = landingTime->getDate();
	int hour = landingTime->getH();
	int min = landingTime->getM();

	//if list is empty, just add Flight to beginning of list
	if (AirlineRep::pendingFlights->empty()){
		AirlineRep::pendingFlights->push_front(*f);
		return 1;
	}

	//for (flightIt = pendingFlights->begin(), advanceIt = pendingFlights->begin(); flightIt != pendingFlights->end() && !done; flightIt++){
	while(flightIt != AirlineRep::pendingFlights->end() && !done){
		
		int currentYear = flightIt->getArrivalTime()->getYear();
		int currentMonth = flightIt->getArrivalTime()->getMonth();
		int currentDate = flightIt->getArrivalTime()->getDate();
		int currentHour = flightIt->getArrivalTime()->getH();
		int currentMin = flightIt->getArrivalTime()->getM();

		if (year > currentYear){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month > currentMonth){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date > currentDate){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date == currentDate && hour > currentHour){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date == currentDate && hour == currentHour && min > currentMin){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}

		flightIt++;
		advanceIt++;
	}

	AirlineRep::pendingFlights->insert(advanceIt, *f);

	return 1;
}

/*------------------------------------------------------------
/ reqWeeklyOutFlight (2.4)
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Adds outgoing Flight to list of weekly Flights
/				waiting to be approved by AirportManager
/ Parameters:	flightID:	unique identifier for Flight
/				aircraftID:	unique identifier for Aircraft carrying out the Flight
				destAirport:identifier for airport the Flight is leaving from
				departureTime:Time Flight is scheduled to depart the Gate
				takeOffTime:Time Flight is scheduled to depart the airport
/ Return: int:	0 for failure; 1 for success
------------------------------------------------------------*/
int AirlineRep::reqWeeklyOutFlight(string flightID, int aircraftID, string
destAirport, /*Time* departureTime, */Time* takeOffTime){
	
	int incoming = 0;
	int flag = 0;					//flag indicating aircraft with given id has been found
	int prepTime = 0;

	Flight *f = new Flight(flightID, aircraftID, /*departureTime, */takeOffTime, "", 0, destAirport, WEEKLY);

	list<Aircraft>::iterator acIt;	//list of Aircraft belonging to ALR
	
	//setting AircraftType of Flight so that a GateStand can be allocated later
	//search through list of Aircraft until Aircraft with given id is found
	for (acIt = AirlineRep::aircraftList->begin(); acIt != AirlineRep::aircraftList->end() && !flag; acIt++){
	
		//when Aircraft is found, raise flag and add Flight to its schedule
		if (acIt->getAircraftID() == aircraftID){
			flag = 1;

			f->setType(acIt->getAircraftType());	//so f can tell a GateStand what type of Aircraft it is
		}
	}

	Time* t = new Time();
	int prepMins = prepTime%60;
	int prepHours = prepTime/60;
	int prepDays = prepTime/1440;
	int prepMonths = 0;
	int m = takeOffTime->getMonth();
	int prepYear = 0;
	int y = takeOffTime->getYear();
	if (m == 2){
		m = prepTime/40320;
	}
	else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
		prepMonths = prepTime/44640;
	}
	else{
		prepMonths = prepTime/43200;
	}

	if (y % 4 == 0){	//leap years mod 4 == 0
		prepYear = prepTime/527040;	//60 mins/hour * 24 hrs/day * 366 days
	}
	else{
		prepYear = prepTime/525600;	//60 mins/hour * 24 hrs/day * 365 days
	}
	
	t->setM(takeOffTime->getM() - prepMins);
	t->setH(takeOffTime->getH() - prepHours);
	t->setDate(takeOffTime->getDate() - prepDays);
	t->setMonth(takeOffTime->getMonth() - prepMonths);
	t->setYear(takeOffTime->getYear() - prepYear);

	f->setDepartureTime(t);

	list<Flight>::iterator flightIt, advanceIt;

	int done = 0;

	//new Flight's scheduled departure time
	int year = takeOffTime->getYear();
	int month = takeOffTime->getMonth();
	int date = takeOffTime->getDate();
	int hour = takeOffTime->getH();
	int min = takeOffTime->getM();

	//if list is empty, just add Flight to beginning of list
	if (AirlineRep::pendingFlights->empty()){
		AirlineRep::pendingFlights->push_front(*f);
		return 1;
	}

	//for (flightIt = pendingFlights->begin(), advanceIt = pendingFlights->begin(); flightIt != pendingFlights->end() && !done; flightIt++){
	while(flightIt != AirlineRep::pendingFlights->end()/* && !done*/){
		
		int currentYear = flightIt->getArrivalTime()->getYear();
		int currentMonth = flightIt->getArrivalTime()->getMonth();
		int currentDate = flightIt->getArrivalTime()->getDate();
		int currentHour = flightIt->getArrivalTime()->getH();
		int currentMin = flightIt->getArrivalTime()->getM();

		if (year > currentYear){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month > currentMonth){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date > currentDate){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date == currentDate && hour > currentHour){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}
		else if (year == currentYear && month == currentMonth && date == currentDate && hour == currentHour && min > currentMin){
			AirlineRep::pendingFlights->insert(advanceIt, *f);
			return 1;
		}

		flightIt++;
		advanceIt++;
	}

	AirlineRep::pendingFlights->insert(advanceIt, *f);

	return 1;
}

/*------------------------------------------------------------
/ changeID (2.7)
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Modifies unique identifier of a Flight
/ Parameters:	f: pointer to Flight for which ID is changing
/			newID:	new unique identifier for Flight
/ Return: int:	0 for failure; 1 for success
------------------------------------------------------------*/
int AirlineRep::changeID(Flight* f, int newID){

	if (AirlineRep::aircraftList->empty()){
		return 0;
	}
	else{

	//get AircraftType of f
	int id = f->getAircraftID();
	
	int oldACFound = 0;
	int newACFound = 0;

	AircraftType *type = 0;
	AircraftType *newType = 0;

	//have aircraft id, now have to get aircraft type
	list<Aircraft>::iterator acIt;
	
	int currentID;
	
	//
	for (acIt = AirlineRep::aircraftList->begin(); acIt != AirlineRep::aircraftList->end() && oldACFound == 0 && newACFound == 0; acIt++){
		currentID = acIt->getAircraftID();
		if (id == currentID){
			type = acIt->getAircraftType();
			oldACFound = 1;
		}	//type of Aircraft being replaced is known
		if (currentID == newID){
			newType = acIt->getAircraftType();
			newACFound = 1;
		}	//type of new Aircraft is known
	}

	//types are not the same, reassignment cannot happen
	if (type != newType){
		return 0;
	}

	//reassign aircraft id to Flight*
	f->setAircraftID(newID);
	
	//function exits successfully
	return 1;
	
	//get AircraftType of Aircraft with id newID
	//if AircraftTypes are same, set f's aircraftID attribute to newID
	}
}

/*------------------------------------------------------------
/ cancelFlight (2.8)
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Designates a Flight as cancelled by moving it from
/				the pendingFlights list to a cancelledFlights list
/ Parameters:	
/ Return: int:	0 for failure; 1 for success
------------------------------------------------------------*/
int AirlineRep::cancelFlight(string _flightID){

	if (AirlineRep::pendingFlights->empty()){
		return 0;	//no Flight to be cancelled
	}

	list<Flight>::iterator flightIt;

	//find Flight being cancelled and add it to list of cancelled Flights
	for (flightIt = AirlineRep::pendingFlights->begin(); flightIt != AirlineRep::pendingFlights->end(); flightIt++){
		if (flightIt->getFlightID().compare(_flightID) == 0){
			AirlineRep::cancelledFlights->push_back(*flightIt);	//adds Flight to list of cancelled Flights
			AirlineRep::pendingFlights->erase(flightIt);		//removes Flight from list of pending Flights
			return 1;
		}
	}

	return 1;
}

/*------------------------------------------------------------
/ issueDelay (2.10)
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Allows AirlineRep to report a delay in a specific Flight
/ Parameters:	f: pointer to Flight that has been delayed
/			delay: amount of delay in minutes
/ Return: int:	0 for failure; 1 for success
------------------------------------------------------------*/
int AirlineRep::issueDelay(Flight* f, int delay){

	f->increaseDelayAmount(delay);

	//need to check if delay affects this Aircraft's other Flights?
	return 1;
}

/*------------------------------------------------------------
/ addAircraft (2.14)
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Adds a new Aircraft to list of Aircraft belonging
/				to the airline this AirlineRep works for
/ Parameters:	_aircraftID:	unique identifier for new Aircraft
/				_type:	pointer to AircraftType (info about this Aircraft)
/ Return: int:	0 for failure; 1 for success
------------------------------------------------------------*/
int AirlineRep::addAircraft(int _aircraftID, AircraftType* _type){

	list<Aircraft>::iterator acIt;

	Aircraft *ac = new Aircraft(_aircraftID, _type, 0, "", this->getAirline());

	if (!(AirlineRep::aircraftList->empty())){
		AirlineRep::aircraftList->push_front(*ac);
		return 1;
	}

	for (acIt = AirlineRep::aircraftList->begin(); acIt != AirlineRep::aircraftList->end(); acIt++){
		if (acIt->getAircraftID() == _aircraftID){	//aircraft with given id already exists in system
			delete ac;
			return 0;	//function returns unsuccessfully
		}
	}

	AirlineRep::aircraftList->push_back(*ac);	//add copy of newly created aircraft to list

	return 1;	//function exits successfully
}

/*------------------------------------------------------------
/ removeAircraft (2.13)
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Removes an Aircraft from the list of Aircraft belonging
/				to the airline this AirlineRep works for
/ Parameters:	_aircraftID:	unique identifier for Aircraft being removed
/ Return: int:	0 for failure; 1 for success
------------------------------------------------------------*/
int AirlineRep::removeAircraft(int _aircraftID){
	
	list<Aircraft>::iterator acIt, eraseIt;

	if (!(AirlineRep::aircraftList->empty()))
		return 0;	//No Aircraft in list; nothing to be deleted

	int index = 0;
	int acFound = 0;

	//going through aircraftList looking for Aircraft with id _aircraftID
	//for each Aircraft in the list that doesn't match an index is advanced
	//by 1 to keep track of the Aircraft's position when it is found
	for (acIt = AirlineRep::aircraftList->begin(); acIt != AirlineRep::aircraftList->end() && acFound == 0; acIt++){
		if (acIt->getAircraftID() != _aircraftID){
			index++;	//current Aircraft is not the one being deleted; advance index by 1 to keep track of where Aircraft is when it is found
		}
		if (acIt->getAircraftID() == _aircraftID){
			acFound = 1;	//flag indicating the Aircraft was found
		}
	}

	//if index is equal to size of list no Aircraft with given id was found
	if (index == AirlineRep::aircraftList->size())
		return 0;	//no Aircraft was deleted

	//advance iterator to position of aircraft that is being deleted
	advance (eraseIt, index);

	//eraseIt should be pointing to Aircraft that is being deleted
	AirlineRep::aircraftList->erase(eraseIt);

	return 1;	//aircraft found and deleted
}


/*------------------------------------------------------------
/ changePassword
/ By: Trevor Plint
/ Date: March 17 2011
/ Description: deletes a stand by ID  
/ Paramneters: takes a valid ID in string form 
/ Return: 0 if stand cannot be deleted for ligistical reasons
          1 if stand deleted successfully
         0 if stand not found
------------------------------------------------------------*/
int AirlineRep::changePassword(string old_pw, string new_pw){
	
	//check the old password
	if (getEmployeePassword ().compare(old_pw)  == 0){
		setEmployeePassword(new_pw);
		return 1; //success
	}
	return 0; //failed
}

/*------------------------------------------------------------
/ emptyList
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: Used by other classes to tell whether or not aircraftList is empty
/				2011/03/27: had to make aircraftList public; this method is no longer necessary but still in use by AirportManager
/ Parameters:
/ Return: int:	0 if list is not empty, 1 if list is empty
------------------------------------------------------------*/
int AirlineRep::emptyList(){
	if (AirlineRep::aircraftList->empty()){
		return 1;	//list is empty
	}
	else
		return 0;
}

/*------------------------------------------------------------
/ updateFlight	(2.9)
/ By: Randy Wiebe
/ Date: March 17 2011
/ Description: changes source/destination airport of Flight depending
/				on whether the Flight is incoming or outgoing
/ Parameters:	f:	pointer to Flight being modified
/ Return: int:	0 for failure; 1 for success
------------------------------------------------------------*/
int updateFlight(Flight *f, string _airport){

	if (f->getIncoming() == 1){
		f->setDestAirport(_airport);
		return 1;
	}
	else{
		f->setSrcAirport(_airport);
		return 1;
	}

	return 0;
}
